﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;
using Avatar.Common;
using System.IO;

public partial class Management_ADManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                DropDownList1.DataBind();
                this.Bind();
                ddl_ADCategory.DataBind();
                ddl_ADCategory.Enabled = false;
                var _adCategory = _adc.ADCategory.Where(c => c.ID == new Guid(ddl_ADCategory.SelectedValue)).FirstOrDefault();
                lbl_WidthHeight.Text = _adCategory.Width.ToString() + "*" + _adCategory.Height.ToString();
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        this.Bind();
    }
    protected void Bind()
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            GridView1.DataSource = _adc.AD.Where(a => a.ADCategoryID == new Guid(DropDownList1.SelectedValue));
            GridView1.DataBind();
        }
    }
    protected void btn_Next_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(ddl_Day.SelectedValue)) throw new Exception("投放周期为空或没有选择。");
        if (string.IsNullOrEmpty(txt_Title.Text)) throw new Exception("广告标题不能为空");

        using (var _adc = DBHelper.GetDataContext())
        {
            var _adid = string.IsNullOrEmpty(txt_ADID.Value) ? Guid.Empty : new Guid(txt_ADID.Value);
            var _ad = _adc.AD.FirstOrDefault(a => a.ID == _adid);
            if (_ad != null)
            {
                _ad.Title = txt_Title.Text;
                if (!string.IsNullOrEmpty(txt_URL.Text)) _ad.URL = txt_URL.Text;

                #region 图片上传
                if (FileUpload1.HasFile)
                {

                    String _savePath = Server.MapPath(_ad.Content.Substring(0, _ad.Content.LastIndexOf('/')) + "\\");

                    string _fileName = _ad.Content.Substring(_ad.Content.LastIndexOf('/') + 1);

                    FileUpload1.PostedFile.SaveAs(_savePath + _fileName);

                }
                #endregion

                #region 广告延期投放

                int _day = Convert.ToInt32(ddl_Day.SelectedValue);
                if (_day > 0)
                {
                    if (_ad.OverDatetime > DateTime.Now)
                    {
                        _ad.OverDatetime = Convert.ToDateTime(_ad.OverDatetime).AddDays(_day);
                    }
                    else
                    {
                        _ad.OverDatetime = DateTime.Now.AddDays(_day);
                    }
                    _ad.Open = true;
                }

                #endregion

                _adc.SubmitChanges();
            }
            txt_Title.Text = "";
            Panel1.Visible = false;
            ddl_Day.ClearSelection();
            this.Bind();
        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Button _btn = sender as Button;
        var _adid = !string.IsNullOrEmpty(_btn.CommandArgument) ? new Guid(_btn.CommandArgument) : Guid.Empty;
        if (_adid != Guid.Empty)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _ad = _adc.AD.FirstOrDefault(a => a.ID == _adid);
                if (_ad != null)
                {
                    ddl_ADCategory.SelectedValue = _ad.ADCategoryID.ToString();
                    txt_ADID.Value = _ad.ID.ToString();
                    txt_Title.Text = _ad.Title;
                    txt_URL.Text = _ad.URL;

                    var _adCategory = _adc.ADCategory.Where(c => c.ID == new Guid(ddl_ADCategory.SelectedValue)).FirstOrDefault();
                    lbl_WidthHeight.Text = _adCategory.Width.ToString() + "*" + _adCategory.Height.ToString();
                }
            }
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
    }
    protected void btn_OpenClose_Click(object sender, EventArgs e)
    {
        Button _btn = sender as Button;
        var _adid = !string.IsNullOrEmpty(_btn.CommandArgument) ? new Guid(_btn.CommandArgument) : Guid.Empty;
        if (_adid != Guid.Empty)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _ad = _adc.AD.FirstOrDefault(a => a.ID == _adid);
                if (_ad != null)
                {
                    _ad.Open = _ad.Open == true ? false : true;

                    _adc.SubmitChanges();
                }
            }
        }
        this.Bind();
    }
    protected void btn_del_Click(object sender, EventArgs e)
    {
        Button _btn = sender as Button;
        var _adid = !string.IsNullOrEmpty(_btn.CommandArgument) ? new Guid(_btn.CommandArgument) : Guid.Empty;
        if (_adid != Guid.Empty)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _ad = _adc.AD.FirstOrDefault(a => a.ID == _adid);
                if (_ad != null)
                {
                    bool _ok = false;
                    if (File.Exists(Server.MapPath(_ad.Content)))
                    {
                        try
                        {
                            File.Delete(Server.MapPath(_ad.Content));
                            _ok = true;
                        }
                        catch
                        {
                            _ok = false;
                        }
                    }
                    else
                    {

                    }
                    if (_ok)
                    {
                        _adc.AD.DeleteOnSubmit(_ad);

                        _adc.SubmitChanges();
                    }
                }
            }
        }
        this.Bind();
    }
}
